package com.guodi.map.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guodi.common.utils.UuidUtil;
import com.guodi.core.boot.ctrl.BaseController;
import com.guodi.core.secure.utils.AuthUtil;
import com.guodi.core.tool.api.R;
import com.guodi.map.entity.MapCustomQuery;
import com.guodi.map.service.IMapCustomQueryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @描述: 自定义查询统计表 前端控制器
 * @作者: 陈宏成
 * @日期: 2021-10-29
 */
@RestController
@RequestMapping("/MapCustomQuery")
@Api(value = "API - MapCustomQueryController", tags = "自定义查询统计表")
public class MapCustomQueryController extends BaseController {

    @Resource
    private IMapCustomQueryService iMapCustomQueryService;

    /**
     * @描述：新增 自定义查询统计表 记录
     * @作者: 陈宏成
     * @日期: 2021-10-29
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增")
    @Transactional(rollbackFor = Exception.class)
    public R<MapCustomQuery> save(@Validated MapCustomQuery mapCustomQuery){
        // 新增前的验证数据，填充数据
        this.prepareBeforeSave(mapCustomQuery);
        // 新增
        boolean save = this.iMapCustomQueryService.save(mapCustomQuery);
        if (save){
            MapCustomQuery entity = this.iMapCustomQueryService.getById(mapCustomQuery.getId());
            return R.data(entity);
        }else{
            throw new RuntimeException("新增失败");
        }
    }

    /**
     * @描述：修改 自定义查询统计表 记录
     * @作者: 陈宏成
     * @日期: 2021-10-29
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改")
    @Transactional(rollbackFor = Exception.class)
    public R<MapCustomQuery> update(@Validated MapCustomQuery mapCustomQuery){
        // 修改前的验证数据，填充数据
        this.prepareBeforeUpdate(mapCustomQuery);
        // 修改
        MapCustomQuery oldEntity = this.iMapCustomQueryService.getById(mapCustomQuery.getId());
        LambdaUpdateWrapper<MapCustomQuery> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.setEntity(oldEntity);
        boolean update = this.iMapCustomQueryService.update(mapCustomQuery ,updateWrapper);
        if (update){
            MapCustomQuery entity = this.iMapCustomQueryService.getById(mapCustomQuery.getId());
            return R.data(entity);
        }else{
            throw new RuntimeException("修改失败");
        }
    }

    /**
     * @描述：删除 自定义查询统计表 记录
     * @作者: 陈宏成
     * @日期: 2021-10-29
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "主键id，如果多个，英文逗号隔开", required = true, dataType = "string", paramType = "query")
    })
    @Transactional(rollbackFor = Exception.class)
    public R<List<MapCustomQuery>> delete(@Validated @NotBlank(message = "ids不能为空") String ids){
        List<String> idList = Arrays.asList(ids.trim().split(","));
        List<MapCustomQuery> mapCustomQuerys = this.iMapCustomQueryService.listByIds(idList);
        boolean remove = this.iMapCustomQueryService.removeByIds(idList);
        if (remove){
            return R.data(mapCustomQuerys);
        }else{
            throw new RuntimeException("删除失败");
        }
    }

    /**
     * @描述：获取 自定义查询统计表 列表
     * @作者: 陈宏成
     * @日期: 2021-10-29
     */
    @PostMapping("/list")
    @ApiOperation(value = "获取列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "size", value = "页面大小，默认10", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页码，默认1",  dataType = "int", paramType = "query"),
    })
    public R<Page<MapCustomQuery>> list(@ApiIgnore Page page){
        LambdaQueryWrapper<MapCustomQuery> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(MapCustomQuery::getCreateTime);
        Page page1 = this.iMapCustomQueryService.page(page, queryWrapper);
        return R.data(page1);
    }

    /**
     * @描述：获取 自定义查询统计表 详情
     * @作者: 陈宏成
     * @日期: 2021-10-29
     */
    @PostMapping("/detail")
    @ApiOperation(value = "获取详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键id", required = true, dataType = "string", paramType = "query")
    })
    public R<MapCustomQuery> detail(@Validated @NotBlank(message = "id不能为空") String id){
        MapCustomQuery mapCustomQuery = this.iMapCustomQueryService.getById(id);
        return R.data(mapCustomQuery);
    }

    /**
     * @描述：修改前的准备，包括验证数据和填充数据
     * @入参：MapCustomQuery
     * @作者: 陈宏成
     * @日期: 2021-10-29
     */
    private void prepareBeforeUpdate(MapCustomQuery mapCustomQuery){
        if(StringUtils.isBlank(mapCustomQuery.getId())){
            throw new RuntimeException("请选择一个自定义查询");
        }
        mapCustomQuery.setModifyTime(new Date());
        mapCustomQuery.setModifier(AuthUtil.getUserId().toString());
    }

    /**
     * @描述：新增前的准备，包括验证数据和填充数据
     * @入参：MapCustomQuery
     * @作者: 陈宏成
     * @日期: 2021-10-29
     */
    private void prepareBeforeSave(MapCustomQuery mapCustomQuery){
        mapCustomQuery.setId(UuidUtil.get32UUID());
        mapCustomQuery.setCreateTime(new Date());
        mapCustomQuery.setCreator(AuthUtil.getUserId().toString());
    }
}
